﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.GetBrowserTokenParameters
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.IO;
using System.ServiceModel.Security;
using System.ServiceModel.Security.Tokens;
using System.Xml;

namespace Microsoft.InfoCards
{
  internal class GetBrowserTokenParameters
  {
    private GetBrowserTokenEndpoint m_recipient;
    private GetBrowserTokenEndpoint m_issuer;
    private Uri m_tokenType;
    private List<string> m_requiredClaims;
    private List<string> m_optionalClaims;
    private int m_flags;
    private uint m_privacyVersion;
    private Uri m_privacyUrl;

    public GetBrowserTokenParameters()
    {
      this.m_recipient = new GetBrowserTokenEndpoint();
      this.m_issuer = new GetBrowserTokenEndpoint();
      this.m_requiredClaims = new List<string>();
      this.m_optionalClaims = new List<string>();
    }

    public GetBrowserTokenEndpoint Recipient
    {
      get
      {
        return this.m_recipient;
      }
    }

    public GetBrowserTokenEndpoint Issuer
    {
      get
      {
        return this.m_issuer;
      }
    }

    public IList<string> RequiredClaims
    {
      get
      {
        return (IList<string>) this.m_requiredClaims.AsReadOnly();
      }
    }

    public IList<string> OptionalClaims
    {
      get
      {
        return (IList<string>) this.m_optionalClaims.AsReadOnly();
      }
    }

    public Uri PrivacyUrl
    {
      get
      {
        return this.m_privacyUrl;
      }
    }

    public uint PrivacyVersion
    {
      get
      {
        return this.m_privacyVersion;
      }
    }

    public void Load(BinaryReader reader, int paramVersion)
    {
      this.m_flags = paramVersion;
      this.m_recipient.Load(reader, paramVersion);
      this.m_issuer.Load(reader, paramVersion);
      string uriString1 = Utility.DeserializeString(reader);
      if (!string.IsNullOrEmpty(uriString1))
        this.m_tokenType = new Uri(uriString1);
      int num1 = reader.ReadInt32();
      for (int index = 0; index < num1; ++index)
        this.m_requiredClaims.Add(Utility.DeserializeString(reader));
      int num2 = reader.ReadInt32();
      for (int index = 0; index < num2; ++index)
        this.m_optionalClaims.Add(Utility.DeserializeString(reader));
      this.m_privacyVersion = reader.ReadUInt32();
      string uriString2 = Utility.DeserializeString(reader);
      if (!string.IsNullOrEmpty(uriString2))
        this.m_privacyUrl = new Uri(uriString2);
      else
        this.m_privacyUrl = (Uri) null;
    }

    public CardSpacePolicyElement CreatePolicyElement(
      ProtocolProfile protocolProfile)
    {
      XmlDocument document = new XmlDocument();
      XmlElement element = document.CreateElement(protocolProfile.WSTrust.DefaultPrefix, protocolProfile.WSTrust.ClaimsElement, protocolProfile.WSTrust.Namespace);
      foreach (string requiredClaim in (IEnumerable<string>) this.RequiredClaims)
        element.AppendChild((XmlNode) GetBrowserTokenParameters.CreateClaimElement(document, requiredClaim, false));
      foreach (string optionalClaim in (IEnumerable<string>) this.OptionalClaims)
        element.AppendChild((XmlNode) GetBrowserTokenParameters.CreateClaimElement(document, optionalClaim, true));
      IssuedSecurityTokenParameters securityTokenParameters = new IssuedSecurityTokenParameters();
      if ((Uri) null != this.m_tokenType)
        securityTokenParameters.TokenType = this.m_tokenType.ToString();
      securityTokenParameters.AdditionalRequestParameters.Add(element);
      Collection<XmlElement> requestParameters = securityTokenParameters.CreateRequestParameters(protocolProfile.MsgSecurityVersion, (SecurityTokenSerializer) new WSSecurityTokenSerializer(protocolProfile.MsgSecurityVersion.SecurityVersion, true, (SamlSerializer) null, (SecurityStateEncoder) null, (IEnumerable<Type>) null));
      foreach (XmlElement xmlElement in requestParameters)
      {
        if (StringComparer.Ordinal.Equals(xmlElement.LocalName, protocolProfile.WSTrust.KeyType) && StringComparer.Ordinal.Equals(xmlElement.NamespaceURI, protocolProfile.WSTrust.Namespace))
        {
          xmlElement.RemoveAll();
          xmlElement.AppendChild((XmlNode) xmlElement.OwnerDocument.CreateTextNode(protocolProfile.WSTrust.KeyTypeBearer.OriginalString));
          break;
        }
      }
      return new CardSpacePolicyElement(this.m_recipient != null ? this.m_recipient.CreateEndpointAddressXml() : (XmlElement) null, this.m_issuer != null ? this.m_issuer.CreateEndpointAddressXml() : (XmlElement) null, requestParameters, this.m_privacyUrl, Convert.ToInt32(this.m_privacyVersion), false);
    }

    private static XmlElement CreateClaimElement(
      XmlDocument document,
      string claimUri,
      bool optional)
    {
      XmlElement element = document.CreateElement("wsid", "ClaimType", "http://schemas.xmlsoap.org/ws/2005/05/identity");
      element.SetAttribute("Uri", claimUri);
      if (optional)
        element.SetAttribute("Optional", "true");
      return element;
    }
  }
}
